// pages/order/order-detail/order-detail/order-detail.js
import { formatTime } from '../../../../utils/util'
import {
  getOrderDetails,
  sendBack,
  sendCancel,
  sendToBeAssigned,
  getDriverImg,
  showRemarks,
  orderTime
} from "../../../../network/client"
import {

  toast
} from '../../../../utils/Tools'
// ../../../utils/Tools
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 退出弹框文字长度
    t_length: 0,
    // 退回弹框显示状态
    sendBack: true,
    // 退回弹框输入的信息
    payCause: '',
    // 弹框显示状态
    isCause: false,

    // 通过弹框显示状态
    flag: true,
    //通过弹框确认状态
    affirm: true,
    //通过弹框文本
    deliveryText: '',
    //通过咨询框确认状态
    consult: '',

    //订单详情数据
    orderInfo: {},
    // 页面参数
    PageParams: '',
    // 激活页面
    activeIndex: '',
    // 司机信息
    DriverImgInfo: "",
    // // 查看单据
    // showRemarksInfo:"",
    // 查看单据文字信息
    base_table: "",
    // 查看单据图片路径
    remarks_pic: "",
    // 时间渲染
    time: 24 * 60 * 60 * 1000,
    // 下单日期
    placeDate: '',

    // 当前日期
    present: '',
    // 渲染时间
    applyTime: "",
    // 定时器名
    loanTime: '',
    order_id: '',
    arrDate: []
  },
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    // console.log("options", options, 1);

    this._getOrderDetails(options.order_id, options.orderType)



    this.setData({
      PageParams: options.order_id,
      activeIndex: options.activeIndex,
      order_id: options.order_id,
      // arrDate:options.arrDate
    })


    this.dispose(JSON.parse(decodeURIComponent(options.arrDate)))

  },
  // 处理时长函数
  dispose(arrDate) {
    let orderId = this.data.order_id
    console.log();
    for (let key in arrDate) {
      console.log("key", key, orderId);
      if (key === orderId) {
        this.setData({
          arrDate: arrDate[orderId]
        })
        // console.log("时长",);
      }
    }
  },
  // 页面卸载时触发
  onUnload: function (options) {

    clearInterval(this.data.loanTime)
  },


  // 时间处理函数
  // tiemDispose: function (faultDate, completeTime) {
  //   let priorTime = Date.parse(new Date(faultDate));
  //   let nowTime = Date.parse(new Date(completeTime));
  //   let usedTime = nowTime - priorTime; //两个时间戳差
  //   // 计算天数
  //   let days = Math.floor(usedTime / (24 * 3600 * 1000));
  //   //计算小时
  //   let leave1 = usedTime % (24 * 3600 * 1000); 
  //   let hours = Math.floor(leave1 / (3600 * 1000));
  //   //计算分钟
  //   let leave2 = leave1 % (3600 * 1000); 
  //   let minutes = Math.floor(leave2 / (60 * 1000));
  //   // 计算秒数
  //   let leave3=leave2%(60*1000)
  //   let second=Math.floor(leave3 / 1000);
  //   // 拼接
  //   let time = days+"天" + hours+"时" + minutes+"分"+second+"秒";
  //   return time;
  //   },


  // 弹框按钮
  onReady: function () {
    this.driver = this.selectComponent("#driver"),
      this.bills = this.selectComponent("#bills")
  },
  // 司机
  showDriver(e) {
    console.log("e", e);
    this.driver.showDriver()
    this._getDriverImg()
  },
  // 司机信息详情请求 //
  async _getDriverImg() {
    const data = {
      order_id: this.data.order_id
    }
    let res = await getDriverImg(data)
    this.setData({
      DriverImgInfo: res.data
    })

  },
  // 查看单据
  showBills() {
    this.bills.showBills()
  },

  //查看单据确认按钮
  _billsSubmit() {

  },
  // 页面跳转 //

  // 分配按钮的跳转
  skipAllocation() {
    wx.navigateTo({
      url: `/pages/order/order-allocation/driver/driver?detail=${this.data.PageParams}`
    })
  },
  // 上传单据按钮跳转
  skipUploading() {
    wx.navigateTo({
      url: `/pages/order/order-underway/uploading/uploading?orderId=${this.data.PageParams}`
    })
  },

  // 隐藏弹窗
  hidePopup: function () {
    this.setData({
      flag: !this.data.flag
    })
  },

  // 隐藏弹窗
  hideSend: function () {
    this.setData({
      sendBack: !this.data.sendBack
    })
  },

  // 退回展示弹窗 //
  showSend: function (e) {
    let deliveryText = e.currentTarget.dataset['text']
    this.setData({
      sendBack: !this.data.sendBack,
      deliveryText
    })
  },
  // 退回提交按钮 //
  async handleSubmitCause() {

    this._sendSelectState()
    this.hideSend()
  },
  // 监听输入框 //
  bindText(e) {
    // 获取输入框长度
    this.setData({
      t_length: e.detail.value.length,
      payCause: e.detail.value
    })
    console.log("输入框内容", e.detail.value);
  },

  /**
   * 通过部分
   */
  // 通过弹出框开关 //
  showPopup() {
    this.setData({
      flag: !this.data.flag
    })
  },
  //  弹框选择项  //
  showAffirm: function (e) {
    this.setData({
      affirm: !this.data.affirm
    })
    let deliveryText = e.currentTarget.dataset['text']
    this.setData({
      deliveryText
    })
  },

  //  咨询框 send//
  async consultPop(e) {
    let consult = e.currentTarget.dataset['consult']
    if (consult === "true") {
      this._sendSelectState()
    } else {
      toast("已取消")
    }
    this.setData({
      affirm: true,
      flag: !this.data.flage
    })
  },

  // 隐藏
  hideAffirm: function () {
    this.setData({
      affirm: !this.data.affirm
    })
  },

  // 点击按钮时处理逻辑
  handleHidden(e) {
    this.setData({
      isCause: true
    })

  },
  //  重新分配网络请求  //
  async _sendToBeAssigned() {
    const data = {
      order_id: this.data.PageParams,
      type: "1"
    }
    const res = await sendToBeAssigned(data)
    if (res.code === 200) {
      toast('操作成功', 'success')
      wx.navigateBack({
        delta: 1,
      })
    } else {
      toast(res.message)
    }
  },
  //  取消订单网络请求  //
  async cancelOrder() {
    const data = {
      order_id: this.data.PageParams,
      edit_name: "cancel",
      type: "2",
      table: "order"
    }
    const res = await sendCancel(data)
    if (res.code === 200) {
      toast('操作成功', 'success')
      wx.navigateBack({
        delta: 1,
      })
    } else {
      toast(res.message)
    }
  },
  //  退回&通过网络请求  //
  async _sendSelectState() {
    if (this.data.deliveryText === "订单退回") {
      var code = 2
    } else {
      var code = 1
    }
    const data = {
      code,
      status: "1",
      get_method: this.data.deliveryText,
      order_id: this.data.PageParams,
      remarks: this.data.payCause,
    }
    const res = await sendBack(data)
    this.setData({
      flag: !this.data.flag
    })
    if (res.code === 200) {
      toast('操作成功', 'success')
      wx.navigateBack({ delta: 1 })
    } else {
      toast(res.message)
    }
  },
  // 请求等待时长
  async _orderTime() {
    const { activeIndex } = this.data
    // console.log("activeIndex", activeIndex);
    // let dataList={}
    if (activeIndex == 2) {
      var dataList = {
        code: 1,
        status: 1,
        preget: 0,
        order_status: 0,
        order_id: this.data.order_id,
      }
    } else if (activeIndex == 3) {
      var dataList = {
        code: 1,
        status: 1,
        preget: 1,
        order_status: 0,
        order_id: this.data.order_id,
        driver_status: 0
      }
    } else if (activeIndex == 4) {
      var dataList = {
        code: 1,
        status: 1,
        preget: 1,
        driver_status: 1,
        order_status: 0,

        order_id: this.data.order_id,


      }
    }
    // const data={
    //   dataList,
    //   // code:1,
    //   // status:1,
    //   // preget:0,
    //   // order_status:0,

    // }
    const data = dataList
    const res = await orderTime(data)
    // console.log("等待时长", res);
    if (res.code === 200) {
      this.setData({
        applyTime: res.data
      })
    }

    // console.log("1")
  },

  //  详情获取网络请求  //
  async _getOrderDetails(params, order) {
    var that = this;
    const data = {
      order_type: order,
      order_id: params
    }
    const res = await getOrderDetails(data)
    console.log("订单详情", res);
    if (res.code === 200) {
      this.setData({
        orderInfo: res.data,
        placeDate: res.data.make_date
      })


    }
    const { activeIndex } = this.data
    if (activeIndex == 2 || activeIndex == 3 || activeIndex == 4) {


      // 一秒钟定时器
      that.data.loanTime = setInterval(function () {
        that._orderTime()
        // var timestamp = Date.parse(new Date()); 
        // that.setData({
        //   applyTime:that.tiemDispose(res.data.make_date, timestamp)
        // })  
      }, 1000)
    }
    // console.log("打印",res.data.make_date);
  }
})